home *** CD-ROM | disk | FTP | other *** search
/ ...taking it to the Macs! / ...taking it to the Macs!.iso / Extras / ActiveX Mac SDK / ActiveX SDK / Container Common / CContainer.cpp < prev    next >
Text File  |  1997-01-03  |  4KB  |  226 lines

  1. //
  2. //    Container.cpp
  3. //
  4. //  Copyright (C) Microsoft Corporation, 1996
  5. //
  6.  
  7. #include "headers.h"
  8. #include "CContainer.h"
  9. #include "CContainerManager.h"
  10. #include "CXEnumGeneric.h"
  11.  
  12.  
  13. //
  14. //  CXContainer::CContainer
  15. //
  16. //  Default constructor
  17. //  If this is used then do a SetContainerParams before doing anything fancy.
  18. //
  19.  
  20. CContainer::CContainer(void)
  21. {
  22.     CommonInit();
  23. }
  24.  
  25.  
  26. //
  27. //  CXContainer::CContainer
  28. //
  29. //  Parameterized constructor
  30. //
  31.  
  32. CContainer::CContainer(CContainerManager* inOwner, PlatformPort* inHostPort)
  33. {
  34.     CommonInit();
  35.  
  36.     mHostPort = inHostPort;
  37.     mOwner = inOwner;
  38. }
  39.  
  40.  
  41. //
  42. //  CXContainer::CommonInit
  43. //
  44. //  fill values that don't vary for constructors
  45. //
  46.  
  47. void
  48. CContainer::CommonInit(void)
  49. {
  50.     mHostPort = NULL;
  51.     mCodeDownloadP = NULL;
  52.     mHostPort = NULL;
  53.     mOwner = NULL;
  54.  
  55.     mSiteArrayP = new LArray( sizeof(void*));;
  56. }
  57.  
  58.  
  59. //
  60. //  CXContainer::~CContainer
  61. //
  62. //  Destructor
  63. //
  64.  
  65. CContainer::~CContainer()
  66. {
  67.     // Get rid of the code download object
  68.     if (mCodeDownloadP != NULL)
  69.     {
  70.         delete mCodeDownloadP;
  71.         mCodeDownloadP = NULL;
  72.     }
  73.  
  74.     // Unlink ourselves from the global list of container objects if there is one
  75.     if ( mOwner )
  76.     {
  77.         mOwner->RemoveContainer( this );
  78.     }
  79.  
  80.     if ( mSiteArrayP )
  81.         delete mSiteArrayP;
  82. }
  83.  
  84.  
  85. //
  86. //  CContainer::IUnknown::QueryInterface
  87. //
  88. //  Get an interface pointer on the object
  89. //
  90.  
  91. STDMETHODIMP
  92. CContainer::QueryInterface(REFIID inRefID, void** outObj)
  93. {
  94.     ErrorCode Result = CBaseCOM::QueryInterface(inRefID, outObj);
  95.     
  96.     if ( Result == E_NOINTERFACE )
  97.     {
  98.         void* pv = nil;
  99.  
  100.         if ( inRefID == IID_IContainer )
  101.             pv = (void*) (IContainer*) this;
  102.         *outObj = pv;
  103.         
  104.         if ( pv )
  105.         {
  106.             ((IUnknown*) pv)->AddRef();
  107.             Result = S_OK;
  108.         }
  109.     }
  110.     
  111.     return Result;
  112.  
  113. }
  114.  
  115.  
  116. //********************** ICollection methods **********************/
  117. //
  118. //  CContainer::ICollection::AddItem
  119. //
  120.  
  121. void
  122. CContainer::AddSite(CXSite *inSite)
  123. {
  124.     mSiteArrayP->InsertItemsAt(1, 0x7fffffff, &inSite);
  125. }
  126.  
  127.  
  128.  
  129. //
  130. //  CContainer::ICollection::RemoveItem
  131. //
  132.  
  133. void
  134. CContainer::RemoveSite(CXSite *inSite)
  135. {
  136.     mSiteArrayP->Remove(&inSite);
  137. }
  138.  
  139.  
  140. //
  141. //  CContainer::ICollection::GetCount
  142. //
  143.  
  144. Uint32
  145. CContainer::GetCount(void)
  146. {
  147.     return mSiteArrayP->GetCount();
  148. }
  149.  
  150.  
  151. //
  152. //  CContainer::ICollection::FetchItem
  153. //
  154.  
  155. void
  156. CContainer::FetchSite(Int16 inIndex, CXSite** outSite)
  157. {
  158.     mSiteArrayP->FetchItemAt(inIndex, outSite);
  159. }
  160.  
  161.  
  162. //
  163. //  CContainer::EnumControls
  164. //
  165. //  return an enumerator of controls in the container which meet the flags requirements
  166. //
  167.  
  168. STDMETHODIMP
  169. CContainer::EnumControls (SearchSpec* /* inSpec */, Uint32 inSearchDepth, IEnumUnknown** outEnumerator)
  170. {
  171.     ErrorCode        theResult;
  172.     Uint32            cElements;
  173.     IUnknown**        punkArray;
  174.     Uint32            current;
  175.     CXSite*            pSite;
  176.     CXEnumGeneric*    penum;
  177.  
  178.     if (inSearchDepth & OLECONTF_EMBEDDINGS) {
  179.         //  Allocate as many elements as we have sites.  Note that this may not
  180.         //  be the true number of objects available if for some reason a site
  181.         //  failed to instantiate its corresponding object.
  182.         Uint32    theSiteCount = mSiteArrayP->GetCount();
  183.  
  184.         if ((punkArray = (IUnknown**) CoTaskMemAlloc(theSiteCount * sizeof(IUnknown*))) == NULL)
  185.         {
  186.             *outEnumerator = NULL;
  187.             return E_OUTOFMEMORY;
  188.         }
  189.  
  190.         current = cElements = 0; 
  191.         while (current < theSiteCount)
  192.         {
  193.             if (mSiteArrayP->FetchItemAt(++current, &pSite) && pSite->m_punkObject != NULL)
  194.                 punkArray[cElements++] = pSite->m_punkObject;
  195.         }
  196.     }
  197.     else
  198.     {
  199.         //  Return an empty enumerator if we're asked for types of objects we
  200.         //  don't contain.
  201.         cElements = 0;
  202.         punkArray = NULL;
  203.     }
  204.  
  205.     if ((penum = new CXEnumGeneric((void*) punkArray, cElements, 
  206.         &g_EnumUnknown)) != NULL) {
  207.         //  Update the reference count of all contained objects.
  208.         for (current = 0; current < cElements; current++)
  209.             punkArray[current]->AddRef();
  210.  
  211.         *outEnumerator = (IEnumUnknown*)(LPENUMGENERIC) penum;
  212.         theResult = S_OK;
  213.     }
  214.     else
  215.     {
  216.         CoTaskMemFree(punkArray);
  217.         *outEnumerator = NULL;
  218.         theResult = E_OUTOFMEMORY;
  219.     }
  220.  
  221.     return theResult;
  222. }
  223.  
  224.  
  225.  
  226.